package com.example.myandroidapp1;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.myandroidapp1.activity.AbstractBaseActivity;

public class SharedPrefs extends AbstractBaseActivity implements
		OnClickListener {

	private EditText etSharedPref;
	private TextView tvShowResults;
	private static final String FILE_NAME = "MySharedData";
	private SharedPreferences sharedData;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sharedpreferences);
		
		etSharedPref = (EditText) findViewById(R.id.etSharedPref);
		tvShowResults = (TextView) findViewById(R.id.tvResultsSharedPref);

		Button loadBtn = (Button) findViewById(R.id.bLoadSharedPref);
		Button saveBtn = (Button) findViewById(R.id.bSaveSharedPref);
		loadBtn.setOnClickListener(this);
		saveBtn.setOnClickListener(this);
		
		sharedData = getSharedPreferences(FILE_NAME, 0);
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.bLoadSharedPref:
			sharedData = getSharedPreferences(FILE_NAME, 0);
			String dataReturn = sharedData.getString("sharedPrefKey", "Default");
			tvShowResults.setText(dataReturn);
			break;
		case R.id.bSaveSharedPref:
			String stringData = etSharedPref.getText().toString();
			Editor editor = sharedData.edit();
			editor.putString("sharedPrefKey", stringData);
			editor.commit();
			break;
		default:
			break;
		}
	}
}
